﻿using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WLYT.Application.Interfaces;
using WLYT.Domain.Model;

namespace WLYT.Application.Services
{
    public class CAEAService(IHttpWebService httpWebService) : ICAEAService
    {
        public async Task<ApiResult> InStockUpload(string reelId, string warehouseNo, string locationNo)
        {
            ApiResult result = new();
            if (string.IsNullOrEmpty(AppConfig.CaeaUrl)) return result;
            string url = $"{AppConfig.CaeaUrl}/api/Order/OutStoreAndBindBox";
            var param = new
            {
                ReelId = reelId,
                WarehouseNo = warehouseNo,
                LocationNo = locationNo,
            };
            try
            {
                result = await httpWebService.HttpPostAsync<ApiResult>(url, param);
            }
            catch (Exception ex)
            {
                result.Code = 1;
                result.Message = ex.Message;
            }
            return result;
        }

        public async Task<ApiResult> OutStockUpload(string orderNo, string reelId, int status = 0)
        {
            ApiResult result = new();
            if (string.IsNullOrEmpty(AppConfig.CaeaUrl)) return result;
            string url = $"{AppConfig.CaeaUrl}/slapi/SCRW.ashx";
            var param = new
            {
                OrderNo = orderNo,
                ReelId = reelId,
                Status = status,
            };
            try
            {
                result = await httpWebService.HttpPostAsync<ApiResult>(url, param);
            }
            catch (Exception ex)
            {
                result.Code = 1;
                result.Message = ex.Message;
            }
            return result;
        }


        public async Task<ApiResult> PickCompleted(string orderNo, string reelId, string boxCode)
        {
            ApiResult result = new();
            if (string.IsNullOrEmpty(AppConfig.CaeaUrl)) return result;
            string url = $"{AppConfig.CaeaUrl}/slapi/SCRW.ashx";
            var param = new
            {
                OrderNo = orderNo,
                ReelId = reelId,
                BoxCode = boxCode,
            };
            try
            {
                result = await httpWebService.HttpPostAsync<ApiResult>(url, param);
            }
            catch (Exception ex)
            {
                result.Code = 1;
                result.Message = ex.Message;
            }
            return result;
        }


        public async Task<ApiResult> BoxArriveEnd(string orderNo, string boxCode)
        {
            ApiResult result = new();
            if (string.IsNullOrEmpty(AppConfig.CaeaUrl)) return result;
            string url = $"{AppConfig.CaeaUrl}/slapi/SCRW.ashx";
            var param = new
            {
                OrderNo = orderNo,
                BoxCode = boxCode,
            };
            try
            {
                result = await httpWebService.HttpPostAsync<ApiResult>(url, param);
            }
            catch (Exception ex)
            {
                result.Code = 1;
                result.Message = ex.Message;
            }
            return result;
        }
    }
}
